package com.yinhai.exception;

import com.yinhai.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @author : chengxd
 * @create 2024/2/20 10:53
 */
@Component("ex")
public class SGExpressionRoot {
    public boolean hasAuthority(String role){
        // 从SpringContext中取出对应的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        Collection<? extends GrantedAuthority> authorities = loginUser.getAuthorities();

        // 判断用户所拥有的权限中是否有
        return authorities.contains(role);

    }
}
